package ticketmaster.dataaccess;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 *
 * @author Wei Liu
 */
public class DataSource
{
    private static String url;
    private static String username;
    private static String password;
    private static boolean isInit = false;

    public static void init() throws IOException, ClassNotFoundException
    {
        Properties properties = new Properties();

        //create a buffered input stream, in order to read properties into
        //variable properties, parameter is file name of property
//        BufferedInputStream in = new BufferedInputStream(DataSource.class.getResourceAsStream("../resources/database.properties"));
//        properties.load(in); // read properties file into variable properties
//        in.close();          // after reading, close the file

        // get database drivers and set system property
        String driver = "com.mysql.jdbc.Driver";
        if(driver != null)          // set system property
            System.setProperty("jdbc.driver", driver);

        url = "jdbc:mysql://localhost:3306/ticket_master";
        username = "root";
        password = "root";

        Class.forName(driver);

        isInit = true;
    }

    public static Connection getConnection() throws SQLException, IOException, ClassNotFoundException
    {
        if(!isInit)
            init();
        return DriverManager.getConnection(url, username, password);
    }
}
